
"""
test the usage of PyUnit
this script mainly works as a documentation to demonstrate the usage of serverl methods of TestCase
"""

import unittest

def global_throw_exeption(x,y):
    if (x == y):
        raise ValueError

class TestPyUnit(unittest.TestCase):

    # ========================================== #
    def double_value(self,x):
        return x * 2
    
    def throw_exception(self,flag):
        if (flag == 0):
            raise ValueError
    
    # ========================================== #
    def test_assert_equal(self):
        self.assertEquals(10,self.double_value(5),"for testing pyunit usage")
        
    def test_almost_equal(self):
        self.assertAlmostEqual(3.141,3.142,1,"almost equal")
        self.assertNotAlmostEqual(3.141,3.142)
        
    def test_assert_raise(self):
        # assertRaise cannot have error message appended
        self.assertRaises(ValueError,self.throw_exception,0)
        self.assertRaises(ValueError,global_throw_exeption,5,5)
        
if __name__ == "__main__":
    unittest.main()
        